package com.example.studentmanager.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.studentmanager.R;
import com.example.studentmanager.adapter.HomeViewPagerAdapter;
import com.example.studentmanager.fargment.CenterFragment;
import com.example.studentmanager.fargment.CourseListFragment;
import com.example.studentmanager.fargment.StudentListFragment;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {

    private TextView tvTitle;
    private ViewPager vpMain;
    private View vStudent;
    private ImageView ivStudent;
    private TextView tvStudent;
    private View vCourse;
    private ImageView ivCourse;
    private TextView tvCourse;
    private View vCenter;
    private ImageView ivCenter;
    private TextView tvCenter;

    private List<Fragment> fragmentList = new ArrayList<>();
    private HomeViewPagerAdapter homeViewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initFragment();
        initView();
        initListener();
        setCurrentHomePagerIndex(0);
    }

    private void initFragment() {
        fragmentList.clear();
        fragmentList.add(StudentListFragment.createFragment());
        fragmentList.add(CourseListFragment.createFragment());
        fragmentList.add(CenterFragment.createFragment());
    }

    private void initListener() {
        vStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentHomePagerIndex(0);
            }
        });
        vCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentHomePagerIndex(1);
            }
        });
        vCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentHomePagerIndex(2);
            }
        });

        vpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCurrentHomePagerIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        tvTitle = findViewById(R.id.tvTitle);
        vpMain = findViewById(R.id.vpMain);
        homeViewPagerAdapter = new HomeViewPagerAdapter(fragmentList, getSupportFragmentManager());
        vpMain.setAdapter(homeViewPagerAdapter);

        vStudent = findViewById(R.id.vStudent);
        ivStudent = findViewById(R.id.ivStudent);
        tvStudent = findViewById(R.id.tvStudent);

        vCourse = findViewById(R.id.vCourse);
        ivCourse = findViewById(R.id.ivCourse);
        tvCourse = findViewById(R.id.tvCourse);

        vCenter = findViewById(R.id.vCenter);
        ivCenter = findViewById(R.id.ivCenter);
        tvCenter = findViewById(R.id.tvCenter);


    }

    public void setCurrentHomePagerIndex(int index) {
        if (index > 3) {
            return;
        }
        vpMain.setCurrentItem(index, false);

        int selectColor = getColor(R.color.main_color);
        int noSelectColor = getColor(R.color.color_333333);
        if (index == 0) {
            tvTitle.setText("同学列表");
            ivStudent.setImageResource(R.drawable.select_student);
            ivCourse.setImageResource(R.drawable.no_select_course);
            ivCenter.setImageResource(R.drawable.no_select_center);
            tvStudent.setTextColor(selectColor);
            tvCourse.setTextColor(noSelectColor);
            tvCenter.setTextColor(noSelectColor);
        } else if (index == 1) {
            tvTitle.setText("课程列表");
            ivStudent.setImageResource(R.drawable.no_select_student);
            ivCourse.setImageResource(R.drawable.select_course);
            ivCenter.setImageResource(R.drawable.no_select_center);
            tvStudent.setTextColor(noSelectColor);
            tvCourse.setTextColor(selectColor);
            tvCenter.setTextColor(noSelectColor);
        } else if (index == 2) {
            tvTitle.setText("个人中心");
            ivStudent.setImageResource(R.drawable.no_select_student);
            ivCourse.setImageResource(R.drawable.no_select_course);
            ivCenter.setImageResource(R.drawable.select_center);
            tvStudent.setTextColor(noSelectColor);
            tvCourse.setTextColor(noSelectColor);
            tvCenter.setTextColor(selectColor);
        }
    }
}